<?php

namespace app\admin\controller;


use app\common\controller\BaseAdmin;
use app\admin\model\User as Model;
use xiaodi\JWTAuth\Facade\Jwt;

class User  extends BaseAdmin
{



    /**
     * 初始化CURD控制器
     * @return void
     */
    public function initialize(){
        parent::initialize();
        $this->model = new Model();
    }


    /**
     * 一键登录
     * @return void
     */
    public function login()
    {
        $id = $this->request->param('id');
        $token = Jwt::token(['uid' => $id]);
        $token = (string)$token;
        $h5_url = env('H5_URL');
        $loginUrl = $h5_url . '/#/?apitoken=' . $token;
        header("Location: $loginUrl");
    }

    //一键重置
    public function reset()
    {    
        $id = $this->request->param('id');
        $user = $this->model->find($id);
        if (empty($user)) {
            $this->error('用户不存在');
        }
        $user->name = '';
        $user->bank_number = '';
        $user->bank_ifsc = '';
        $res = $user->save();
        if($res){
            $this->success("用户{$user->name}一键重置操作成功");
        }else{
            $this->error('用户不存在');
        }
    }


    public function pass(){
        $data = $this->request->param();
        $user = $this->model->where('id', $data['id'])->find();
        if (empty($user)) {
            $this->error('用户不存在');
        }
        if ($this->request->isAjax()){
            $user->password =password_hash($data['password'], PASSWORD_BCRYPT,['cost' => 10]);
            if(!$user->save()){
                $this->error('更新失败');
            }
            $this->success("更新成功");
        }

        $this->assign('info', $user);
        return $this->fetch();
    }

    public function updateAmount(){
        $data = $this->request->param();
        $user = $this->model->where('id', $data['id'])->find();
        if (empty($user)) {
            $this->error('用户不存在');
        }
        if($this->request->isAjax()){
            if($data['updatetype'] === '1'){
            $user->money = $user->money+$data['amount'];
            $user->remark = $data['remark'];
            }else{
            $user->money = $user->money-$data['amount'];
            $user->remark = $data['remark'];
            }
            if(!$user->save()){
                $this->error('更新失败');
            }
            $this->success('更新成功');
        }
        $this->assign('info',$user);
        return $this->fetch();
    }


}